<?php

namespace app\cms\controller;

use app\cms\model\CmsWareInbound;
use app\data\model\BaseUserUpgrade;
use app\data\model\DataUser;
use app\data\model\DataUserBalance;
use app\data\model\DataUserRebate;
use app\data\model\ShopGoods;
use app\data\model\ShopOrder;
use think\admin\Controller;

/**
 * 数据统计
 * Class Portal
 * @package app\cms\controller\total
 */
class Portal extends Controller
{
  /**
   * 数据统计页面
   * @auth true
   * @menu true
   */
  public function index()
  {
    
    $this->weightTotal = CmsWareInbound::mk()->cache(true, 60)->where(['deleted' => 0])->sum('total_weight');
    $this->amountTotal = CmsWareInbound::mk()->cache(true, 60)->where(['deleted' => 0])->sum('total_price');
    // 近十天的用户及交易趋势
    $this->days = $this->app->cache->get('portals', []);
    if (empty($this->days)) {
      for ($i = 15; $i >= 0; $i--) {
        $date = date('Y-m-d', strtotime("-{$i}days"));
        $this->days[] = [
          '当天日期' => date('m-d', strtotime("-{$i}days")),
          '增加用户' => CmsWareInbound::mk()->whereLike('created_at', "{$date}%")->sum('total_weight'),
          '订单数量' => CmsWareInbound::mk()->whereLike('created_at', "{$date}%")->sum('total_price'),
          
        ];
      }
      $this->app->cache->set('portals', $this->days, 60);
    }
   
    $this->fetch();
  }
}
